#!/usr/bin/env Rscript

rm(list = ls())

setwd("../data/voter_file_aggregated")

## this script calculates summaries for the left table in Figure 3

load("individual_partychanges_summary_withpartydirection_beforeaftermoves_registeredallyears_nonmovers.RData")
INDIV.partySummary.nonmovers.before <- INDIV.partySummary.movers.before #future movers
INDIV.partySummary.nonmovers.after <- INDIV.partySummary.movers.after #already moved
load("individual_partychanges_summary_withpartydirection_beforeaftermoves_registeredallyears_movers.RData")

myWeightedMean <- function(changeparty,majorparty,forweights) {
    weights <- forweights / sum(forweights)
    return(weighted.mean(changeparty / majorparty, weights, na.rm=T))
}

myWeightedSD <- function(changeparty,majorparty,forweights) {
    weights <- forweights / sum(forweights)
    my.weighted.sd <- function(p=changeparty/majorparty,n=majorparty,w=weights) {
        sqrt(
            sum((w / sum(w * n))^2
             * n * p * (1 - p)
             )
            )
    }
    return(my.weighted.sd())
}

NEVERMOVED08 <- myWeightedMean(
    changeparty=subset(
        INDIV.partySummary.nevermoved[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.nevermoved[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty
    )

ALLMOVERS08 <- myWeightedMean(
    changeparty=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty
    )

NONMOVERS08.before <- myWeightedMean(
    changeparty=subset(
        INDIV.partySummary.nonmovers.before[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.nonmovers.before[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty
    )

NONMOVERS08.after <- myWeightedMean(
    changeparty=subset(
        INDIV.partySummary.nonmovers.after[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.nonmovers.after[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty
    )

DOUBLEMOVERS08.before <- myWeightedMean(
    changeparty=subset(
        INDIV.partySummary.movers.before[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.movers.before[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty
    )

DOUBLEMOVERS08.after <- myWeightedMean(
    changeparty=subset(
        INDIV.partySummary.movers.after[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.movers.after[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty
    )



NEVERMOVED10 <- myWeightedMean(
    changeparty=subset(
        INDIV.partySummary.nevermoved[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.nevermoved[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty
    )

ALLMOVERS10 <- myWeightedMean(
    changeparty=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty
    )

NONMOVERS10.before <- myWeightedMean(
    changeparty=subset(
        INDIV.partySummary.nonmovers.before[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.nonmovers.before[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty
    )

NONMOVERS10.after <- myWeightedMean(
    changeparty=subset(
        INDIV.partySummary.nonmovers.after[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.nonmovers.after[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty
    )

DOUBLEMOVERS10.before <- myWeightedMean(
    changeparty=subset(
        INDIV.partySummary.movers.before[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.movers.before[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty
    )

DOUBLEMOVERS10.after <- myWeightedMean(
    changeparty=subset(
        INDIV.partySummary.movers.after[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.movers.after[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty
    )

MEANs <- t(data.frame(
    nevermoved = mean(NEVERMOVED08 + NEVERMOVED10),
    allmovers = mean(ALLMOVERS08 + ALLMOVERS10),
    nonmovers.before = mean(NONMOVERS08.before + NONMOVERS10.before),
    nonmovers.after = mean(NONMOVERS08.after + NONMOVERS10.after),
    doublemovers.before = mean(DOUBLEMOVERS08.before + DOUBLEMOVERS10.before),
    doublemovers.after = mean(DOUBLEMOVERS08.after + DOUBLEMOVERS10.after)
    ))

MEANs
